<template>
  <div class="login_container">
    <div class="login_box">
      <div class="login_gravatar">
        <img src="@/assets/logo.png" alt />
      </div>
      <el-form
        :model="loginForm"
        :rules="loginFormRules"
        ref="loginForm"
        label-width="80px"
        id="loginForm"
      >
        <el-form-item prop="username" label="用户名：">
          <el-input
            placeholder="请输入用户名..."
            clearable
            v-model="loginForm.username"
            suffix-icon="el-icon-user"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password" label="密码：">
          <el-input
            placeholder="请输入密码..."
            clearable
            show-password
            suffix-icon="el-icon-edit"
            v-model="loginForm.password"
          ></el-input>
        </el-form-item>
        <el-form-item style="position:relative;left:50%;transform:translate(-37%)">
          <el-button type="primary" @click="submitLoginForm">登录</el-button>
          <el-button @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
//导入配置文件
import { SERVER_API_IP } from "../config/index";
export default {
  name: "Login",
  data() {
    return {
      // 登陆表单数据绑定对象
      loginForm: {
        // 用户名
        username: "",
        // 密码
        password: ""
      },
      // 登录表单校验规则
      loginFormRules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          {
            min: 5,
            max: 16,
            message: "用户名长度需在 5 到 16 个字符",
            trigger: "blur"
          }
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 6,
            max: 24,
            message: "密码长度需在 6 到 24 个字符",
            trigger: "blur"
          }
        ]
      }
    };
  },
  methods: {
    //重置登录表单信息
    resetLoginForm() {
      this.$refs["loginForm"].resetFields();
    },
    //提交登录表单
    async submitLoginForm() {
      //提交前验证下表单参数
      this.$refs["loginForm"].validate(
        //如果valid验证成功
        valid =>
          valid &&
          (async () => {
            //发送请求
            let { data } = await this.$http.post("/login", this.loginForm);
            //判断用户状态
            let status = data.meta.status;
            if (status == 200) {
              this.$message.success(data.meta.msg);
              //将token存入sessionStorage中
              window.sessionStorage.setItem("vues_token", data.data.token);
              //存入后跳转路由到首页
              this.$router.push("/home");
            } else {
              this.$message.error(data.meta.msg);
            }
          })()
      );
    }
  }
};
</script>

<style scoped>
.login_container {
  background-color: #2b4b6b;
  height: 100%;
  width: 100%;
}
.login_box {
  position: absolute;
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 30px;
  box-shadow: 2px 5px 5px #888888;
  top: 50%;
  left: 50%;
  margin-left: -225px;
  margin-top: -150px;
}
.login_gravatar {
  height: 130px;
  width: 130px;
  border: 1px solid #eee;
  border-radius: 50%;
  padding: 10px;
  box-shadow: 0 0 10px #888888;
  position: relative;
  left: 50%;
  margin-left: -70px;
  top: -70px;
}
.login_gravatar > img {
  width: 100%;
  height: 100%;
  border-radius: 50%;
  background-color: #eee;
}
#loginForm {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 60px 0 30px;
  box-sizing: border-box;
}
</style>